package com.top10.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.top10.bean.PageBean;
import com.top10.bean.ResponseBean;
import com.top10.common.Const;
import com.top10.entity.Order;
import com.top10.entity.OrderDetailed;
import com.top10.entity.SpecialPrice;
import com.top10.entity.UserCart;
import com.top10.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author sny
 * @Package com.top10.controller.mobile
 * @date 2018/9/29
 */
@RestController
@RequestMapping("/top10/specialprice")
public class SpecialPriceController {


    @Autowired
    private ISpecialPriceService iSpecialPriceService;
    @Autowired
    IUserCartService userCartService;
    @Autowired
    private IOrderDetailedService iOrderDetailedService;
    @Autowired
    private IOrderService iOrderService;

    //    /**
//     * 闪购限时抢购
//     * @param pageBean
//     * @return
//     */
//    @PostMapping("/shanggouseckill")
//    public ResponseBean shanggoulist(PageBean pageBean){
//        Page page = PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
//        List<SpecialPrice> seckill=iSpecialPriceService.selectList(new EntityWrapper<SpecialPrice>().eq("type", Const.ShongGouType)
//                                                                                                    .eq("isdeleted",Const.SpecialIsNotDeleted)
//                                                                                                    .ge("End_time",new Date()));
//        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
//        return ResponseBean.createSuccess()
//                .addData("seckill",seckill)
//                .addData("pageBean",pageBean);
//    }
//
//    /**
//     * 团购限时抢购
//     * @param pageBean
//     * @return
//     */
    @PostMapping("/tuangouseckill")
    public ResponseBean tuangoulist(PageBean pageBean){
        Page page = PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<SpecialPrice> seckill=iSpecialPriceService.selectList(new EntityWrapper<SpecialPrice>().eq("type", Const.TuanGouType)
                .eq("isdeleted",Const.SpecialIsNotDeleted)
                .ge("End_time",new Date()));
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess()
                .addData("seckill",seckill)
                .addData("pageBean",pageBean);
    }


    /**
     * 闪购,团限时抢购列表
     * @param pageBean
     * @return
     */
    @PostMapping("/seckill")
    public ResponseBean seckilllist(Long type,PageBean pageBean){
        if(type==null){
            return ResponseBean.createError("type不能为空");
        }
        int status=1;
        Page page = PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String, Object>> seckill=iSpecialPriceService.selectGoodsByType(type);
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess()
                .addData("seckill",seckill)
                .addData("pageBean",pageBean).addData("status",status);
    }

    /**
     * 按照销量，默认降序排序
     * @param type
     * @param sortid
     * @param pageBean
     * @return
     */

    @PostMapping("/sortseckill")
    public ResponseBean sortseckilllist(Long type, @RequestParam(value="sortid",defaultValue="1")long sortid, PageBean pageBean){
        if(type==null){
            return ResponseBean.createError("type不能为空");
        }
        if (sortid == 1) {
            Page page = PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
            List<Map<String, Object>> seckill=iSpecialPriceService.selectGoodsBysort(type);
            pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess()
                    .addData("seckill",seckill)
                    .addData("pageBean",pageBean);
        }else {
            Page page = PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
            List<Map<String, Object>> seckill=iSpecialPriceService.selectGoodsBysortasc(type);
            pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess()
                    .addData("seckill",seckill)
                    .addData("pageBean",pageBean);
        }

    }

    @PostMapping("/sortbyprice")
    public ResponseBean sortbyprice(Long type, @RequestParam(value="sortid",defaultValue="1")long sortid, PageBean pageBean){
        if(type==null){
            return ResponseBean.createError("type不能为空");
        }
        if (sortid == 1) {
            Page page = PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
            List<Map<String, Object>> seckill=iSpecialPriceService.selectGoodsByprice(type);
            pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess()
                    .addData("seckill",seckill)
                    .addData("pageBean",pageBean);
        }else {
            Page page = PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
            List<Map<String, Object>> seckill=iSpecialPriceService.selectGoodsBypriceasc(type);
            pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess()
                    .addData("seckill",seckill)
                    .addData("pageBean",pageBean);
        }

    }


    /***
     * 1为闪购购包邮
     * @param type
     * @param pageBean
     * @return
     */

    @PostMapping("/listbyexpress")
    public ResponseBean listqqgbyexpress(Long type,PageBean pageBean){

        if(type==null){
            return ResponseBean.createError("type不能为空");
        }
        Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        List<Map<String, Object>> qgoods = iSpecialPriceService.selectGoodsByexpress(type);
        pageBean = new PageBean(pageBean.getPageNum(), pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("goods",qgoods).addData("pageBean",pageBean);

    }
   /* *
     * 删除闪购商品
     */
    @PostMapping("/deletedspecialgood")
    public ResponseBean deletedSpecial(String goodsname,String goodsUuid,@RequestParam(value="minNumber",required = false)Integer minNumber,Integer type) {
        if (type == 1) {
            SpecialPrice specialPrice = new SpecialPrice();
            specialPrice.setGoodsUuid(goodsUuid);
            specialPrice.setMinNumber(minNumber);
            iSpecialPriceService.deletedSpecial(specialPrice);
            //删除购物车
            List<UserCart> userCartList = userCartService.selectList(new EntityWrapper<UserCart>().eq("goods_uuid", goodsUuid));
            for (UserCart userCart : userCartList) {
                userCart.setIsdeleted(true);
                userCartService.updateById(userCart);
            }
            //删除订单
            List<String> orderid = iOrderDetailedService.selectOrderUuid(goodsname);
            for (String orderid1 : orderid) {
                Order order = iOrderService.selectOne(new EntityWrapper<Order>().eq("uuid", orderid1).eq("status", 0));
                if (order != null) {
                    order.setIsdeleted(true);
                    iOrderService.updateById(order);
                }
                Order order1 = iOrderService.selectOne(new EntityWrapper<Order>().eq("uuid", orderid1).eq("status", -1));
                if (order1 != null) {
                    order1.setIsdeleted(true);
                    iOrderService.updateById(order1);
                }
                Order order2 = iOrderService.selectOne(new EntityWrapper<Order>().eq("uuid", orderid1).eq("status", 6));
                if (order2 != null) {
                    order2.setIsdeleted(true);
                    iOrderService.updateById(order2);
                }
            }


        }

                if (type==2){
                      iSpecialPriceService.deletedShenGou(goodsUuid);
                    //删除购物车
                    List<UserCart> userCartList = userCartService.selectList(new EntityWrapper<UserCart>().eq("goods_uuid", goodsUuid));
                    for (UserCart userCart : userCartList) {
                        userCart.setIsdeleted(true);
                        userCartService.updateById(userCart);
                    }
                    //删除订单
                    List<String> orderid = iOrderDetailedService.selectOrderUuid(goodsname);
                    for (String orderid1 : orderid) {
                        Order order = iOrderService.selectOne(new EntityWrapper<Order>().eq("uuid", orderid1).eq("status", 0));
                        if (order != null) {
                            order.setIsdeleted(true);
                            iOrderService.updateById(order);
                        }
                        Order order1 = iOrderService.selectOne(new EntityWrapper<Order>().eq("uuid", orderid1).eq("status", -1));
                        if (order1 != null) {
                            order1.setIsdeleted(true);
                            iOrderService.updateById(order1);
                        }
                        Order order2 = iOrderService.selectOne(new EntityWrapper<Order>().eq("uuid", orderid1).eq("status", 6));
                        if (order2 != null) {
                            order2.setIsdeleted(true);
                            iOrderService.updateById(order2);
                        }
                    }
                }
                return ResponseBean.createSuccess("删除成功");
    }
}